<template>
  <div>
    <el-dialog  :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')"
      :close-on-click-modal="false" :close-on-press-escape="false">
      <el-col :span="24">
        <el-form v-loading="loading" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()"
          :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
          <el-col :span="24">
            <el-form-item label="流程名称" prop="procName">
              <el-select v-model="dataForm.procName" filterable placeholder="请选择流程"
                @change="(key)=> {processChange(key,dataForm)}">
                <el-option v-for="item in processData" :key="item.key" :label="item.name" :value="item.key">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="$t('bpmAgent.agentRealName')" prop="agentRealName">
              <el-input v-model="dataForm.agentRealName" placeholder="请选择" @focus="showUser">
                <el-button slot="append" icon="el-icon-search" @click.stop="showUser"></el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="$t('bpmAgent.startDate')" prop="startDate">
              <!--日期框-->
              <el-date-picker v-model="dataForm.startDate" value-format="yyyy-MM-dd HH:mm:ss"
                :placeholder="$t('bpmAgent.startDate')" type="date"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="$t('bpmAgent.endDate')" prop="endDate">
              <!--日期框-->
              <el-date-picker v-model="dataForm.endDate" value-format="yyyy-MM-dd HH:mm:ss"
                :placeholder="$t('bpmAgent.endDate')" type="date"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="$t('common.remark')" prop="remark">
              <!--文本框-->
              <el-input v-model="dataForm.remark" :placeholder="$t('common.remark')" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-form>
      </el-col>
      <template slot="footer">
        <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
        <el-button type="primary" :loading="loading" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
      </template>
    </el-dialog>
    <uni-user-select v-if="dialogVisible" ref="userSelect" @backFun="userBackFun" :width="'70%'" :multiple="false"
      :initDataForm="initDataForm"></uni-user-select>
  </div>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      loading: false,
      visible: false,
      // 选人控件参数
      initDataForm: {}, // number 类型丢失精度id暂时用string类型
      dialogVisible: false,
      values: [],
      labels: '',
      dataForm: {
        id: '',
        procKey: '',
        procName: '',
        username: '',
        agentName: '',
        agentRealName: '',
        startDate: '',
        endDate: '',
        remark: '',
        creator: '',
        createDate: '',
        users: []
      },
      // 流程地址
      getProcessListURL: '/act/process/list',
      // 流程数据
      processData: [{
        key: '',
        name: '正在加载...'
      }]
    }
  },
  computed: {
    dataRule () {
      return {
        procName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        agentRealName: [
          { required: true, message: this.$t('validate.required'), trigger: 'click' }
        ],
        startDate: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        endDate: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
        // 获取流程
        this.getProcess()
      })
    },

    showUser () {
      this.dialogVisible = true
      // 等待DOM完成更新 v-if 渲染后执行
      this.$nextTick(() => {
        this.$refs.userSelect.init()
      })
    },
    userBackFun (data) {
      this.dataForm.agentName = data[0].username
      this.dataForm.agentRealName = data[0].realName
    },
    // 获取流程列表
    getProcess () {
      this.$http.get(this.getProcessListURL).then(({ data: res }) => {
        if (res.code !== 0) {
          return
        }
        this.processData = res.data
      }).catch(() => { })
    },
    // select选择回调
    processChange (key, model) {
      let obj = {}
      obj = this.processData.filter(item => (item['key'] || '').toString() === (key || '').toString())[0]
      this.dataForm.procName = obj.name
      this.dataForm.procKey = obj.key
    },
    // 获取信息
    getInfo () {
      this.loading = true
      this.$http.get(`/flowable/bpmAgent/${this.dataForm.id}`).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => { })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.loading = true
        this.$http[!this.dataForm.id ? 'post' : 'put']('/flowable/bpmAgent/', this.dataForm).then(({ data: res }) => {
          this.loading = false
          if (res.code !== 0) {
            return
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => { })
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
